# -*- coding: utf-8 -*-
'''
@author: olivia.dou
Created on: 2022/10/20 16:43
desc: 主页面
'''

from appium.webdriver.common.appiumby import AppiumBy

from base.po.mobile_base_page import MobileBasePage
from proj_spec.CLUE.mobile_po.accuisit_by_park_page import AccuisitByParkPage
from proj_spec.CLUE.mobile_po.accuisit_by_subject_page import AccuisitBySubjectPage
from proj_spec.CLUE.mobile_po.clue_app_base_page import CLUEAppBasePage
from proj_spec.CLUE.mobile_po.company_detail_page import CompanyDetailPage
from proj_spec.CLUE.mobile_po.followed_company_page import FollowedCompanyPage
from proj_spec.CLUE.mobile_po.my_page import MyPage
from proj_spec.CLUE.mobile_po.potential_customer_page import PotentialCustomerPage
from proj_spec.CLUE.mobile_po.search_page import SearchPage
from proj_spec.CLUE.mobile_po.surround_customer_page import SurroundCustomerPage


class MainPage(CLUEAppBasePage):
    #_search_locator = (AppiumBy.CLASS_NAME, "android.widget.EditText")
    #_search_locator_android = (AppiumBy.XPATH, "//*[@resource-id='search.index']/*[1]/*[2]/*[1]")
    _search_locator_android = (AppiumBy.XPATH, "//*[@resource-id='search.index']/*[1]/*[2]")
    _search_locator_ios = (AppiumBy.IOS_PREDICATE, 'type == "XCUIElementTypeSearchField"')

    _follow_locator_android = (AppiumBy.XPATH, "//*[contains(@resource-id,'tabTV') and contains(@text,'关注企业')]")
    _follow_locator_ios = (AppiumBy.IOS_CLASS_CHAIN, '**/XCUIElementTypeButton[`label == "关注企业"`]')

    #_potential_customer_locator = (AppiumBy.XPATH, "//*[contains(@resource-id,'tabTV') and contains(@text,'潜客营销')]")
    _potential_customer_locator_android = (AppiumBy.XPATH, "//*[@text='潜客营销']")
    _potential_customer_locator_ios = (AppiumBy.IOS_CLASS_CHAIN, '**/XCUIElementTypeButton[`label == "潜客营销"`]')

    _my_locator_android = (AppiumBy.XPATH, "//*[contains(@resource-id,'tabTV') and contains(@text,'我的')]")
    _my_locator_ios = (AppiumBy.IOS_CLASS_CHAIN, '**/XCUIElementTypeButton[`label == "我的"`]')
    #_acc_by_subject_locator = (AppiumBy.XPATH, "//*[contains(@text, '专题获客')]")
    _acc_by_subject_locator_android = (AppiumBy.ANDROID_UIAUTOMATOR, 'UiSelector().text("专题获客")')
    _acc_by_subject_locator_ios = (AppiumBy.IOS_PREDICATE, 'label == "专题获客"')
    #_surround_customer_locator = (AppiumBy.XPATH, "//*[contains(@text, '周边获客')]")
    _surround_customer_locator = (AppiumBy.ANDROID_UIAUTOMATOR, 'UiSelector().text("周边获客")')
    #_recommend_com_locator = (AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().textContains("推荐企业")')
    _com_from_recommened_locator = (AppiumBy.XPATH,"//*[contains(@text,'推荐企业')]/following-sibling::*[2]/child::*[3]")
    _acc_by_park_locator_android = (AppiumBy.ANDROID_UIAUTOMATOR, 'UiSelector().text("园区获客")')



    def to_search(self):
        #self.find_element(self._search_locator).click()
        self.find_element_and_click(getattr(self, '_search_locator_' + self.os))
        return SearchPage(self.driver)


    def to_my(self):
        # 切换到“我的”页面
        self.find_element_and_click(getattr(self, '_my_locator_' + self.os))
        return MyPage(self.driver)

    def to_followed(self):
        # 切换到关注企业页面
        self.find_element_and_click(getattr(self, '_follow_locator_' + self.os))
        return FollowedCompanyPage(self.driver)

    def to_potential_customer(self):
        # 切换到潜客营销页面
        # 企业微信版默认page source不包含页面下方按钮，需设置allowInvisibleElements为True，点击后恢复原设置
        # 参考http://appium.io/docs/en/commands/session/settings/update-settings/#example-usage，http://appium.io/docs/en/advanced-concepts/settings/index.html
        self.driver.update_settings({ "allowInvisibleElements" : True })
        self.find_element_and_click(getattr(self, '_potential_customer_locator_' + self.os))
        self.driver.update_settings({"allowInvisibleElements": False})
        return PotentialCustomerPage(self.driver)

    def to_acc_by_subject(self):
        """跳转到专题获客页面

        :return:
        """
        self.find_element_and_click(getattr(self, '_acc_by_subject_locator_' + self.os))
        return AccuisitBySubjectPage(self.driver)

    def to_surround_customer(self, grant_location=True):
        """切换到周边获客页面
        :param grant_location: 是否授权使用用户当前地理位置， 默认True

        :return:
        """
        self.find_element_and_click(self._surround_customer_locator)
        sur_cust_page = SurroundCustomerPage(self.driver)
        sur_cust_page.handle_location_request(grant_location)
        return sur_cust_page

    def to_acc_by_park(self):
        """切换到园区获客页面

        :return:
        """
        self.find_element_and_click(getattr(self, '_acc_by_park_locator_' + self.os))
        return AccuisitByParkPage()

    def to_company_detail_from_recommend(self):
        """ 从推荐企业中第一家公司进入企业详情页面

        :return:
        """
        # 因企业微信（也可能其他机型上）推荐企业第一家公司未显示，需统一先小幅滚动屏幕
        #self.swipe_up((AppiumBy.ID, "android:id/content"), 1/5)
        self.find_element((AppiumBy.ANDROID_UIAUTOMATOR,
                                     'new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollForward()'))
        self.find_element_and_click(self._com_from_recommened_locator)
        return CompanyDetailPage(self.driver)

    def scroll_to_top(self):
        # 部分用例需要向下滚动屏幕，在测试类tear down中调用本方法恢复滚动到顶部
        self.find_element((AppiumBy.ANDROID_UIAUTOMATOR,
                                         'new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView'
                                         '(new UiSelector().resourceIdMatches("search.index"));'))

